import dbase using "D:\Dropbox\Research\Management and learning\Datos\RawData\Planea\PB_2018\directores_ctx_18.dbf", clear



gen Cursos=(R007=="1") if !missing(R007)
label var Cursos "Ever"
gen Curso12Meses=(R009=="1") if !missing(R009)
label var Curso12Meses "Past 12 months"

replace Cursos=. if Curso12Meses==.
replace Curso12Meses=. if Cursos==.


gen PlanEstudio=(R078=="3" | R078=="4") if !missing(R078)
label var PlanEstudio "Activities to improve learning"

gen ObservacionAula=(R083=="3" | R083=="4") if !missing(R083)
label var ObservacionAula "Classroom observations"

gen HabilidadesPedagogicas=(R085=="3" | R085=="4") if !missing(R085)
label var HabilidadesPedagogicas "Help teachers improve pedagogical practices"

gen InfoPadres=(R087=="3" | R087=="4") if !missing(R087)
label var InfoPadres "Provide parents with performance information"



rename CCT school_id


*********************
**Base de controles**
*********************


merge 1:1 school_id using "$basein/Datos adicionales/base_Contraste.dta", gen(planea)

*keep if planea==3 | planea==1

**********************
***Base experimento***
**********************

merge 1:1 school_id using "$basein/Datos adicionales/Experimento_id.dta", gen(experimento)

*keep if experimento==3 | experimento==1

******************
***Base estrato***
******************

merge 1:1 school_id using "$basein/Datos adicionales/estratos.dta", gen(_estratos)

*keep if _estratos==3 | _estratos==1

******************
***Base DWMS***
******************

merge 1:1 school_id using "$base_out/DWMS_Regresiones", gen(_DWMS)
*keep if _DWMS==3 | _DWMS==1


***Elimino secundarias***

drop if secondary==1
drop if treatgroup2=="Control"
drop if treatgroup2==""
drop if PEC_PLUS==.
***************************************************
***************************************************
*****Regresiones básicas indice y componentes******
***************************************************
***************************************************
drop Marg
gen Marg=(marginacion=="Muy alta o Alta") if !missing(marginacion)
foreach var of varlist urbana Marg  str insc_tot  ln_math25 ln_lang25 ln_mathlow25 ln_langlow25 lage lagep infra{
	gen miss_`var'=missing(`var')
	replace `var'=0 if missing(`var')
}
global varcontrol Marg##c.miss_Marg  c.urbana c.str c.insc_tot  c.ln_math25##c.miss_ln_math25 c.ln_lang25 c.ln_mathlow25 c.lage##c.miss_lage c.lagep c.infra



preserve
reg Cursos $varcontrol i.estrato if PEC_PLUS==0
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

reg  management_sted3 propensity if PEC_PLUS==0
reg  management_sted3 propensity if PEC_PLUS==1

twoway (scatter management_sted3 propensity if PEC_PLUS==0, color("orange")) (lfit management_sted3 propensity if PEC_PLUS==0, color("orange") lwidth("medthick") lpattern(dash)) (scatter management_sted3 propensity if PEC_PLUS==1, color("ebblue")) (lfit management_sted3 propensity if PEC_PLUS==1, color("ebblue") lwidth("medthick") lpattern(dash)),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
	
graph export "$graphs/PropensityContexto_Management.pdf", replace 
restore

preserve
probit Cursos $varcontrol i.estrato if PEC_PLUS==0
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

reg  management_sted3 propensity if PEC_PLUS==0
reg  management_sted3 propensity if PEC_PLUS==1

twoway (scatter management_sted3 propensity if PEC_PLUS==0, color("orange")) (lfit management_sted3 propensity if PEC_PLUS==0, color("orange") lwidth("medthick") lpattern(dash)) (scatter management_sted3 propensity if PEC_PLUS==1, color("ebblue")) (lfit management_sted3 propensity if PEC_PLUS==1, color("ebblue") lwidth("medthick") lpattern(dash)),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
	
graph export "$graphs/PropensityContexto_Management_probit.pdf", replace 
restore

preserve
logit Cursos $varcontrol i.estrato if PEC_PLUS==0
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

reg  management_sted3 propensity if PEC_PLUS==0
reg  management_sted3 propensity if PEC_PLUS==1

twoway (scatter management_sted3 propensity if PEC_PLUS==0, color("orange")) (lfit management_sted3 propensity if PEC_PLUS==0, color("orange") lwidth("medthick") lpattern(dash)) (scatter management_sted3 propensity if PEC_PLUS==1, color("ebblue")) (lfit management_sted3 propensity if PEC_PLUS==1, color("ebblue") lwidth("medthick") lpattern(dash)),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
	
graph export "$graphs/PropensityContexto_Management_logit.pdf", replace 
restore






probit Cursos $varcontrol i.estrato if PEC_PLUS==0 & treatgroup!="Control" & state!=10, asis
predict propensity
replace propensity=0 if propensity<=0 & !missing(propensity)
replace propensity=1 if propensity>=1 & !missing(propensity)

logit Cursos $varcontrol i.estrato if PEC_PLUS==0 & treatgroup!="Control" & state!=10, asis
predict propensity2
replace propensity2=0 if propensity2<=0 & !missing(propensity2)
replace propensity2=1 if propensity2>=1 & !missing(propensity2)

reg Cursos $varcontrol i.estrato if PEC_PLUS==0 & treatgroup!="Control" & state!=10
predict propensity3
replace propensity3=0 if propensity3<=0 & !missing(propensity3)
replace propensity3=1 if propensity3>=1 & !missing(propensity3)

eststo clear
eststo : reghdfe  management_sted3 PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
eststo : reghdfe  management_sted3 c.propensity##c.PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
replace propensity=propensity2
eststo : reghdfe  management_sted3 c.propensity##c.PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)
replace propensity=propensity3
eststo : reghdfe  management_sted3 c.propensity##c.PEC_PLUS if treatgroup!="Control" & state!=10, a(estrato#enumerador)  vce(cluster school_id)

label var propensity "P(Attending training)"
label var PEC_PLUS "Direct training"



esttab using "$tables/Propensity_ENLANCEContexto_Management.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	keep(*PEC_PLUS* *propensity*) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
	
	
eststo clear
eststo : ivreghdfe management_sted3 (Cursos=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)
eststo : ivreghdfe management_sted3 (Curso12Meses=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)



merge 1:m school_id using "$base_out/alum_pb_2018_modified", gen(_planea) update


eststo :ivreghdfe prom_pca (Cursos=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)
eststo : ivreghdfe prom_pca (Curso12Meses=PEC_PLUS) if treatgroup!="Control" & state!=10, a(estrato#enumerador)  cluster(school_id)
estadd scalar Ftest=e(widstat)

esttab using "$tables/IV_ContextoSurvey.tex" , se ar2 booktabs label b(%9.2gc) se(%9.2gc) nocon nonumber /// 
star(* 0.10 ** 0.05 *** 0.01) fragment ///
replace mlabels(none)  collabels(none) nogaps nolines ///
keep(Cursos Curso12Meses) ///
stats(N  Ftest, fmt(%9.0gc %9.2gc) ///
labels("N. of obs." "F test (first stage)"))

/*
reghdfe  prom_pca c.propensity##c.PEC_Plus if treatgroup!="Control" & state!=10, a(estrato)  vce(cluster cct_num)
gen tag=e(sample)
reghdfe  prom_pca PEC_Plus if  tag & treatgroup!="Control" & state!=10, a(estrato)  vce(cluster cct_num)

collapse (mean) prom_pca propensity PEC_Plus (count) N=prom_pca, by(school_id)
twoway (scatter prom_pca propensity if PEC_Plus==0, color("orange")) (lfit prom_pca propensity [aw=N] if PEC_Plus==0, color("orange")) (scatter prom_pca propensity if PEC_Plus==1, color("ebblue")) (lfit prom_pca propensity [aw=N] if PEC_Plus==1, color("ebblue")),  legend(label(1 "Indirect") label(3 "Direct") order(1 3) col(2) pos(2) ring(0) symxsize(5)  size(large) on region(lstyle(none))) ytitle("Management score at endline") xtitle("Propensity score")
